package com.xinyiweidai.xinyiweidai.module.bind;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.lan.mylibrary.okhttp.L;
import com.lan.mylibrary.okhttp.OkHttpUtils;
import com.lan.mylibrary.okhttp.callback.StringCallback;
import com.lan.mylibrary.ui.MyTitle;
import com.lan.mylibrary.ui.MyToast;
import com.xinyiweidai.xinyiweidai.R;
import com.xinyiweidai.xinyiweidai.base.BaseActivity;
import com.xinyiweidai.xinyiweidai.bean.IdentityBean;
import com.xinyiweidai.xinyiweidai.event.BindingEvent;
import com.xinyiweidai.xinyiweidai.util.Api;
import com.xinyiweidai.xinyiweidai.util.SpUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Request;

/**
 * 项目名：com.xinyiweidai.xinyiweidai.module.bind
 * 类名： IdentityActivity
 * 创建人：Lan
 * 创建时间：2018/9/8 22:18
 * 描述：  身份信息
 */
public class IdentityActivity extends BaseActivity {


    private EditText et_identity_name;
    private EditText et_identity_id;
    private Button btn_identity_next;
    private String name, idCard;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_identity;
    }

    @Override
    protected void initToolBar() {
        new MyTitle(this, R.drawable.ic_back, "身份认证", 0, true);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        et_identity_name = (EditText) findViewById(R.id.et_identity_name);
        et_identity_id = (EditText) findViewById(R.id.et_identity_id);
        btn_identity_next = (Button) findViewById(R.id.btn_identity_next);
    }

    @Override
    protected void addClickEvent() {
        btn_identity_next.setOnClickListener(view -> checkIdentity());
    }


    private void checkIdentity() {
        name = et_identity_name.getText().toString();
        idCard = et_identity_id.getText().toString().trim();

        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(idCard)) {
            MyToast.showError(this, "输入有误");
            return;
        }
        if (idCard.length() != 18) {
            MyToast.showError(this, "号码错误");
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("userid", SpUtil.getInstance().getUserInfo().getUser_id());
        map.put("idCard", idCard);
        map.put("name", name);
        goCheck(map);
    }


    private void goCheck(Map<String, String> map) {
        showLoading("正在认证");
        OkHttpUtils.get()
                .url(Api.idAuthentication)
                .params(map)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        dismissLoading();
                        MyToast.showError(IdentityActivity.this, "网络异常");
                    }

                    @Override
                    public void onResponse(String response) {
                        L.e(response);
                        dismissLoading();
                        parseData(response);
                    }
                });
    }


    private void parseData(String response) {
        IdentityBean bean = new Gson().fromJson(response, IdentityBean.class);
        if (bean.getCode() == 1) {
            MyToast.showSuccess(IdentityActivity.this, "认证成功");
            EventBus.getDefault().post(new BindingEvent("status_1"));
            SpUtil.getInstance().saveString("name",name);
            SpUtil.getInstance().saveString("idCard",idCard);
            startNewActivity(CardActivity.class);
            finish();
        } else {
            MyToast.showError(IdentityActivity.this, bean.getMsg());
        }
    }
}
